home *** CD-ROM | disk | FTP | other *** search
/ Aminet 7 / Aminet 7 - August 1995.iso / Aminet / misc / math / gpamiga_1_38_3.lha / elisp / convert68k.el next >
Encoding:
Text File  |  1993-09-18  |  3.5 KB  |  105 lines

  1. ;;;; convert68k.el
  2. ;;;; A program to process a m68000 assembler file and
  3. ;;;; convert local labels in the form 3$: to non local labels L37:
  4. ;;;; Main use: to convert the mp.s file in the PARI package to
  5. ;;;; something that gcc can understand. The local labels has caused problems
  6. ;;;; both on my amiga and on a Sun3 that I have tried.
  7.  
  8. (defvar global-label-expr "^[a-zA-Z_]+[a-zA-Z0-9_]*:"
  9.   "Regular expression that matches a global label, for example
  10. _cget:" )
  11.  
  12. (defvar local-label-expr "^\\([0-9]+\\$\\)"
  13.   "Regular expression that matches a local label, for example
  14. 3$:" )
  15.  
  16. (defvar convert68k-label-prefix "L_"
  17.   "Prefix that is added to every generated label.")
  18.  
  19. (defvar convert68k-indirect-expr "sp@(\\([0-9]+\\))@\\([^(]\\)"
  20.   "Regular expression that matches the double indirect
  21. construction sp@(number)@")
  22.  
  23. (defvar convert68k-indirect-replace "sp@(\\1)@(0)\\2"
  24.   "Replace string for replace-regexp. Depending on
  25. convert68k-indirect-expt")
  26.  
  27. (defvar convert68k-buffer-name "*convert68k-output*"
  28.   "Name of buffer created by convert68k.")
  29.  
  30. (defun convert68k ()
  31.   "Processes buffer with 68k assembler, to remove all
  32. local labels. Opens a new buffer for output. Also converts
  33. some double-indirect expressions"
  34.   (interactive)
  35.   (let ((convert68k-buffer (get-buffer-create convert68k-buffer-name))
  36.     (label-number 1)
  37.     (source-buffer (current-buffer))
  38.     (global-list nil)
  39.     (last (point-max)))
  40.     ;; Create list of positions for global labels.
  41.     ;; For convenience, the list has the format
  42.     ;; ((1 . 10) (10 . 90) (90 . 400))
  43.     ;; if the labels are located at positions 10 and 90,
  44.     ;; or if the file starts with a label and they are at
  45.     ;; 1, 10 and 90.
  46.     (message "Scanning input for global labels...")
  47.     (goto-char last)
  48.     (while (re-search-backward global-label-expr nil t)
  49.       (setq global-list (cons (cons (point) last)
  50.                   global-list))
  51.       (setq last (point)))
  52.     (cond ((> last (point-min))
  53.        (setq global-list (cons (cons (point-min) last)
  54.                    global-list))
  55.        (setq last (point))))
  56.     (switch-to-buffer-other-window convert68k-buffer)
  57.     (erase-buffer)
  58.     (mapcar '(lambda (pair)
  59.            (convert68k-locals (car pair)
  60.                   (cdr pair)))
  61.         global-list)
  62.     (convert68k-indirect)))
  63.  
  64. (defun convert68k-locals (source-start source-end)
  65.   "Copies the start end region from source-buffer, and converts all
  66. local labels in that region. Assumes that there are no global labels in
  67. the middle of the region."
  68.   (let ((local-list nil)
  69.     (start (point))
  70.     (end (make-marker)))
  71.     (insert-buffer-substring source-buffer
  72.                  source-start
  73.                  source-end)
  74.     (set-marker end (point))
  75.     ;; Create list of local labels. 
  76.     ;; Format: (("1$" . "L_1") ("9$" . "L_2"))
  77.     ;; The car of each pair is the name of the local label,
  78.     ;; the cdr is the new global name.
  79.     (goto-char start)
  80.     (while (re-search-forward local-label-expr end t)
  81.       (setq local-list
  82.         (cons (cons (buffer-substring (match-beginning 1)
  83.                       (match-end 1))
  84.             (format "%s%d" convert68k-label-prefix
  85.                 label-number))
  86.           local-list))
  87.       (setq label-number (+ 1 label-number)))
  88.     (narrow-to-region start end)
  89.     (mapcar '(lambda (pair)
  90.            (goto-char (point-min))
  91.            (replace-string (car pair) (cdr pair) t )
  92.            nil)
  93.         local-list)
  94.     (widen)
  95.     (goto-char end)
  96.     (set-marker end nil)))
  97.  
  98. (defun convert68k-indirect ()
  99.   "Converts the construction sp@(number)@
  100. to sp(number)@"
  101.   (interactive)
  102.   (goto-char (point-min))
  103.   (query-replace-regexp convert68k-indirect-expr
  104.             convert68k-indirect-replace))
  105.